var FKeyPad =$('#Keypad');
var ReadOut = $('#ReadOut');
var Accum = "0";
var FlagNewNum = false;
var PendingOp = "";
//===============================================================================
//[描述] 浮点数精确计算
//[参数]     str1 - 第一个数
// str2 - 第二个数
// type - 运算符
// precision - 小数位精度
//[调用方式]   longCount(str1,str2,type,precision);
//[返回值]   计算结果
//===============================================================================
function longCount(str1,str2,type) {
    var comma1 = 0;
    if (str1.indexOf(".")!=-1) {
        str1 = str1.replace(/0*$/,"");
        comma1 = str1.length - str1.indexOf(".")-1;
    }
    var comma2 = 0;
    if (str2.indexOf(".")!=-1) {
        str2 = str2.replace(/0*$/,"");
        comma2 = str2.length - str2.indexOf(".")-1;
    }
    str1 = str1.replace(/\./,"");
    str2 = str2.replace(/\./,"");
    var value,comma;
    if (type!="*") {
        if (comma1>comma2) {
            for (var i=0;i<comma1-comma2;i++) str2 += "0";
            comma = (type=="/")?0:comma1;
        }else {
            for (var i=0;i<comma2-comma1;i++) str1 += "0";
            comma = (type=="/")?0:comma2;
        }
    }else {
        comma = comma1 + comma2;
    }
    if (type=="+") {
        value = parseInt(str1,10) + parseInt(str2,10);
    }else if (type=="-") {
        value = parseInt(str1,10) - parseInt(str2,10);
    }else if (type=="*") {
        value = parseInt(str1,10) * parseInt(str2,10);
    }else if (type=="/") {
        value = parseInt(str1,10) / parseInt(str2,10);
    }
    value = String(value);
    if (comma>0) value = value.substring(0,value.length-comma)+"."+value.substring(value.length-comma,value.length);
    if (value.indexOf(".")!=-1)
        value = value.replace(/0*$/,"");
    return value;
}
function NumPressed (Num) {
 if (FlagNewNum) {
	 $('#ReadOut').attr("value",Num);
  FlagNewNum = false;
   }
 else {
  if ($('#ReadOut').val() == "0")
	  $('#ReadOut').attr("value",Num);
  else
	  $('#ReadOut').attr("value",$('#ReadOut').val()+Num);
   }
}
function Operation (Op) {
 var Readout = $('#ReadOut').val();
 if (FlagNewNum && PendingOp != "=");
 else
 {
  FlagNewNum = true;
  if ( '+' == PendingOp || '-' == PendingOp || '/' == PendingOp || '*' == PendingOp)
   Accum = longCount(Accum,Readout,PendingOp);
  else
   Accum = Readout;
  $('#ReadOut').attr("value",Accum);
        PendingOp = Op;
        $('#ReadOut').focus();
        $('#ReadOut').select();
 }
}
function Decimal () {
 var curReadOut = $('#ReadOut').val();
 if (FlagNewNum) {
  curReadOut = "0.";
  FlagNewNum = false;
   }
 else
 {
  if (curReadOut.indexOf(".") == -1)
   curReadOut += ".";
   }
 $('#ReadOut').attr("value",curReadOut);
}
function ClearEntry () {
	$('#ReadOut').attr("value","0");
 FlagNewNum = true;
}
function Clear () {
 Accum = "0";
 PendingOp = "";
 ClearEntry();
}
function Neg () {
	$('#ReadOut').attr("value",longCount($('#ReadOut').val(),"-1","*"));
}
function Percent () {
	$('#ReadOut').attr("value",longCount($('#ReadOut').val(),Accum,"*"));
	$('#ReadOut').attr("value",longCount($('#ReadOut').val(),100,"/"));
}
function done(){
	Calc.picker($('#ReadOut').val());
}
/*function goReturn() {
    top.returnValue = FKeyPad.ReadOut.value;
    self.close();
}*/
function CheckOut() {
    var keyCode = window.event.keyCode;
    if (keyCode>=48 && keyCode<=57) {
  if (FlagNewNum) {
	  $('#ReadOut').attr("value","");
   //window.event.keyCode = null;
   FlagNewNum = false;
  }
        return true;
    }else if (keyCode==43 || keyCode==45 || keyCode==42 || keyCode==47 || keyCode==61) {
        Operation(String.fromCharCode(keyCode));
    }else if (keyCode==46) {//.
        if ($('#ReadOut').val().indexOf(".") == -1)
         return true;
    }else if (keyCode==13) goReturn();
    window.event.returnValue = false;
    return false;
}
/*var FKeyPad = document.forms['Keypad'];
var Accum = "0";
var FlagNewNum = false;
var PendingOp = "";
//===============================================================================
//[描述] 浮点数精确计算
//[参数]     str1 - 第一个数
// str2 - 第二个数
// type - 运算符
// precision - 小数位精度
//[调用方式]   longCount(str1,str2,type,precision);
//[返回值]   计算结果
//===============================================================================
function longCount(str1,str2,type) {
    var comma1 = 0;
    if (str1.indexOf(".")!=-1) {
        str1 = str1.replace(/0*$/,"");
        comma1 = str1.length - str1.indexOf(".")-1;
    }
    var comma2 = 0;
    if (str2.indexOf(".")!=-1) {
        str2 = str2.replace(/0*$/,"");
        comma2 = str2.length - str2.indexOf(".")-1;
    }
    str1 = str1.replace(/\./,"");
    str2 = str2.replace(/\./,"");
    var value,comma;
    if (type!="*") {
        if (comma1>comma2) {
            for (var i=0;i<comma1-comma2;i++) str2 += "0";
            comma = (type=="/")?0:comma1;
        }else {
            for (var i=0;i<comma2-comma1;i++) str1 += "0";
            comma = (type=="/")?0:comma2;
        }
    }else {
        comma = comma1 + comma2;
    }
    if (type=="+") {
        value = parseInt(str1,10) + parseInt(str2,10);
    }else if (type=="-") {
        value = parseInt(str1,10) - parseInt(str2,10);
    }else if (type=="*") {
        value = parseInt(str1,10) * parseInt(str2,10);
    }else if (type=="/") {
        value = parseInt(str1,10) / parseInt(str2,10);
    }
    value = String(value);
    if (comma>0) value = value.substring(0,value.length-comma)+"."+value.substring(value.length-comma,value.length);
    if (value.indexOf(".")!=-1)
        value = value.replace(/0*$/,"");
    return value;
}
function NumPressed (Num) {
 if (FlagNewNum) {
	 alert(FlagNewNum);
  FKeyPad.ReadOut.value  = Num;
  FlagNewNum = false;
   }
 else {
  if (FKeyPad.ReadOut.value == "0")
   FKeyPad.ReadOut.value = Num;
  else
   FKeyPad.ReadOut.value += Num;
   }
}
function Operation (Op) {
 var Readout = FKeyPad.ReadOut.value;
 if (FlagNewNum && PendingOp != "=");
 else
 {
  FlagNewNum = true;
  if ( '+' == PendingOp || '-' == PendingOp || '/' == PendingOp || '*' == PendingOp)
   Accum = longCount(Accum,Readout,PendingOp);
  else
   Accum = Readout;
        FKeyPad.ReadOut.value = Accum;
        PendingOp = Op;
        FKeyPad.ReadOut.focus();
        FKeyPad.ReadOut.select();
 }
}
function Decimal () {
 var curReadOut = FKeyPad.ReadOut.value;
 if (FlagNewNum) {
  curReadOut = "0.";
  FlagNewNum = false;
   }
 else
 {
  if (curReadOut.indexOf(".") == -1)
   curReadOut += ".";
   }
 FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
 FKeyPad.ReadOut.value = "0";
 FlagNewNum = true;
}
function Clear () {
 Accum = "0";
 PendingOp = "";
 ClearEntry();
}
function Neg () {
 FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,"-1","*");
}
function Percent () {
 FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,Accum,"*");
 FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,100,"/");
}
function done(){
	Calc.picker(FKeyPad.ReadOut.value);
}
function goReturn() {
    top.returnValue = FKeyPad.ReadOut.value;
    self.close();
}
function CheckOut() {
    var keyCode = window.event.keyCode;
    if (keyCode>=48 && keyCode<=57) {
  if (FlagNewNum) {
   FKeyPad.ReadOut.value  = "";
   //window.event.keyCode = null;
   FlagNewNum = false;
  }
        return true;
    }else if (keyCode==43 || keyCode==45 || keyCode==42 || keyCode==47 || keyCode==61) {
        Operation(String.fromCharCode(keyCode));
    }else if (keyCode==46) {//.
        if (FKeyPad.ReadOut.value.indexOf(".") == -1)
         return true;
    }else if (keyCode==13) goReturn();
    window.event.returnValue = false;
    return false;
}*/